/*
 *
 * Copyright (C) 2012 Walter Lütgenau
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package offset.nodes.server.embedded.view;

import java.util.Arrays;
import java.util.HashSet;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;
import javax.swing.UnsupportedLookAndFeelException;

/**
 *
 * @author Walter L�tgenau
 */
public class StartupLookAndFeelManager {
    static StartupLookAndFeelManager instance = new StartupLookAndFeelManager();

    /**
     * Set the look and feel.
     *
     * Currently, this is nimbus on UNIX, else the system look and feel. If no such l&f is present,
     * use the java l&f.
     *
     * @throws ClassNotFoundException
     * @throws InstantiationException
     * @throws IllegalAccessException
     * @throws UnsupportedLookAndFeelException
     */
    public static void setLookAndFeel() throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException {
        instance.internalSetLookAndFeel();
    }

    /**
     * Set the look and feel.
     *
     * Currently, this is nimbus on UNIX, else the system look and feel. If no such l&f is present,
     * use the java l&f.
     *
     * @throws ClassNotFoundException
     * @throws InstantiationException
     * @throws IllegalAccessException
     * @throws UnsupportedLookAndFeelException
     */
    protected void internalSetLookAndFeel() throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException {
        String lookAndFeel = null;

        if (isUnix())
            lookAndFeel = getNimbus();

        if (lookAndFeel == null)
            lookAndFeel = UIManager.getSystemLookAndFeelClassName();

        if (lookAndFeel == null)
            lookAndFeel = UIManager.getCrossPlatformLookAndFeelClassName();

        UIManager.setLookAndFeel(lookAndFeel);
    }

    /**
     * Return the name of the nimbus look and feel, if present
     *
     * @return
     */
    protected String getNimbus() {
        for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName()))
                return info.getClassName();
        }

        return null;
    }

    /**
     * Is this a UNIX derivative?
     *
     * @return
     */
    protected boolean isUnix() {
        HashSet<String> unixOs = new HashSet<String>(Arrays.asList(new String[] {
            "linux", "solaris", "hp-ux", "aix", "freebsd", "irix", "digitalunix", "osf1"
        }));

        if (unixOs.contains(System.getProperty("os.name").toLowerCase()))
            return true;
        return false;
    }


}
